home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Fast HTML 3151710272001.psc / Form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-27  |  9.8 KB  |  269 lines

  1. VERSION 5.00
  2. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"
  3. Begin VB.Form Form1 
  4.    BackColor       =   &H00808080&
  5.    Caption         =   "Fast HTML Highlight7"
  6.    ClientHeight    =   5520
  7.    ClientLeft      =   60
  8.    ClientTop       =   345
  9.    ClientWidth     =   10050
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5520
  12.    ScaleWidth      =   10050
  13.    StartUpPosition =   3  'Windows Default
  14.    Begin VB.CommandButton cmdregx 
  15.       Caption         =   "Run Regx Only"
  16.       Height          =   375
  17.       Left            =   4080
  18.       TabIndex        =   5
  19.       Top             =   3840
  20.       Width           =   1455
  21.    End
  22.    Begin VB.CommandButton Command3 
  23.       Caption         =   "Un-Highlight"
  24.       Height          =   375
  25.       Left            =   2760
  26.       TabIndex        =   3
  27.       Top             =   3840
  28.       Width           =   1095
  29.    End
  30.    Begin VB.CommandButton Command2 
  31.       Caption         =   "Highlight All"
  32.       Height          =   375
  33.       Left            =   1680
  34.       TabIndex        =   2
  35.       Top             =   3840
  36.       Width           =   1095
  37.    End
  38.    Begin VB.CommandButton Command1 
  39.       Caption         =   "Highlight Selected"
  40.       Height          =   375
  41.       Left            =   120
  42.       TabIndex        =   1
  43.       Top             =   3840
  44.       Width           =   1575
  45.    End
  46.    Begin RichTextLib.RichTextBox rtf1 
  47.       CausesValidation=   0   'False
  48.       Height          =   3735
  49.       Left            =   120
  50.       TabIndex        =   0
  51.       Top             =   0
  52.       Width           =   7095
  53.       _ExtentX        =   12515
  54.       _ExtentY        =   6588
  55.       _Version        =   393217
  56.       ScrollBars      =   3
  57.       TextRTF         =   $"Form1.frx":0000
  58.    End
  59.    Begin VB.Label lbltagcount 
  60.       BorderStyle     =   1  'Fixed Single
  61.       Height          =   375
  62.       Left            =   5880
  63.       TabIndex        =   4
  64.       Top             =   3840
  65.       Width           =   1335
  66.    End
  67. Attribute VB_Name = "Form1"
  68. Attribute VB_GlobalNameSpace = False
  69. Attribute VB_Creatable = False
  70. Attribute VB_PredeclaredId = True
  71. Attribute VB_Exposed = False
  72. 'Fast HTML Highlight
  73. '--------------------------------------------------
  74. 'Copyright 2001 DGS http://www.2dgs.com
  75. 'Written by Gary Varnell
  76. 'You may use this code freely as long as the above
  77. 'copyright info remains intact
  78. '==================================================
  79. ' Needs reference to Microsoft VBscript Regular Expressions.
  80. ' Get it at http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/733/msdncompositedoc.xml
  81. Option Explicit
  82. Dim apppath As String
  83. Dim starttime As Date
  84. Dim tmpchr As String * 1
  85. Dim tmpint As Long
  86. Dim color(3) As Variant
  87. Function colorhtml()
  88. '-----------------------------------------------
  89. 'Define Regularexpressions for colorize function
  90. '-----------------------------------------------
  91. 'regx for Tags
  92.     Dim TagregEx, Match, Matches   ' Create variable.
  93.     Set TagregEx = New RegExp      ' Create a regular expression.
  94.     TagregEx.Pattern = "<(.)[^> ]*( ){0,1}[^>]*>"   ' Set pattern.
  95.     TagregEx.IgnoreCase = False    ' Set case insensitivity.
  96.     TagregEx.Global = True         ' Set global applicability.
  97. 'regx for property="value" pairs
  98.     Dim tagPNregEx, Match2, Matches2    ' Create variable.
  99.     Set tagPNregEx = New RegExp         ' Create a regular expression.
  100.     tagPNregEx.Pattern = "(\w+ *=) *(\d+|""[^""]+"")"   ' tag propertyname.
  101.     tagPNregEx.IgnoreCase = False       ' Set case insensitivity.
  102.     tagPNregEx.Global = True            ' Set global applicability.
  103. '---------------------------------------------
  104. Dim rtfstart As Long
  105. rtfstart = rtf1.SelStart ' Remember startpos since user might have selected text
  106. If rtf1.SelLength < 1 Then
  107.     MsgBox "No text selected"
  108. Exit Function
  109. End If
  110. '----------------------------------------------
  111.     Set Matches = TagregEx.Execute(rtf1.SelText)    ' Execute search.
  112.     For Each Match In Matches     ' Iterate Matches collection.
  113.         If Match.Value <> "" Then 'used to stop empty string match return
  114.             rtf1.SelStart = rtfstart + Match.FirstIndex
  115.             rtf1.SelLength = Match.Length
  116.             rtf1.SelColor = color(0)
  117.             ' now run some short circuit logic
  118.             If Match.SubMatches(0) = "!" Then ' looks like a comment
  119.                rtf1.SelColor = color(3)
  120.                GoTo nextmatch
  121.             ElseIf Match.SubMatches(1) <> " " Then ' this tag doesn't have properties
  122.                 GoTo nextmatch
  123.             End If
  124.             Set Matches2 = tagPNregEx.Execute(Match.Value) ' Execute search.
  125.             For Each Match2 In Matches2
  126.                 If Match2.Value <> "" Then 'used to stop empty string match return
  127.                     'Debug.Print Match2.Value & Match2.Length
  128.                     rtf1.SelStart = Match.FirstIndex + rtfstart + Match2.FirstIndex
  129.                     rtf1.SelLength = Match2.Length
  130.                     rtf1.SelColor = color(2)
  131.                     rtf1.SelLength = Len(Match2.SubMatches(0))
  132.                     rtf1.SelColor = color(1)
  133.                 End If
  134.             Next
  135.         End If
  136. nextmatch:
  137.     Next
  138.     lbltagcount.Caption = Matches.Count & " Tags"
  139. End Function
  140. Function regxonly()
  141. '-----------------------------------------------
  142. 'Define Regularexpressions for colorize function
  143. '-----------------------------------------------
  144. 'regx for Tags
  145.     Dim TagregEx, Match, Matches   ' Create variable.
  146.     Set TagregEx = New RegExp      ' Create a regular expression.
  147.     TagregEx.Pattern = "<(.)[^> ]*( ){0,1}[^>]*>"   ' Set pattern.
  148.     TagregEx.IgnoreCase = False    ' Set case insensitivity.
  149.     TagregEx.Global = True         ' Set global applicability.
  150. 'regx for property="value" pairs
  151.     Dim tagPNregEx, Match2, Matches2    ' Create variable.
  152.     Set tagPNregEx = New RegExp         ' Create a regular expression.
  153.     tagPNregEx.Pattern = "(\w+ *=) *(\d+|""[^""]+"")"   ' tag propertyname.
  154.     tagPNregEx.IgnoreCase = False       ' Set case insensitivity.
  155.     tagPNregEx.Global = True            ' Set global applicability.
  156. '---------------------------------------------
  157. Dim rtfstart As Long
  158. rtfstart = rtf1.SelStart ' Remember startpos since user might have selected text
  159. If rtf1.SelLength < 1 Then
  160.     MsgBox "No text selected"
  161. Exit Function
  162. End If
  163. '----------------------------------------------
  164.     Set Matches = TagregEx.Execute(rtf1.SelText)    ' Execute search.
  165.     For Each Match In Matches     ' Iterate Matches collection.
  166.         If Match.Value <> "" Then 'used to stop empty string match return
  167.             ' now run some short circuit logic
  168.             If Match.SubMatches(0) = "!" Then ' looks like a comment
  169.                GoTo nextmatch
  170.             ElseIf Match.SubMatches(1) <> " " Then ' this tag doesn't have properties
  171.                 GoTo nextmatch
  172.             End If
  173.             Set Matches2 = tagPNregEx.Execute(Match.Value) ' Execute search.
  174.             For Each Match2 In Matches2
  175.                 If Match2.Value <> "" Then 'used to stop empty string match return
  176.                 End If
  177.             Next
  178.         End If
  179. nextmatch:
  180.     Next
  181.     lbltagcount.Caption = Matches.Count & " Tags"
  182. End Function
  183. Private Sub cmdregx_Click()
  184. starttime = Time
  185. Me.MousePointer = vbHourglass
  186. rtf1.Visible = False
  187. rtf1.SelStart = 0
  188. rtf1.SelLength = Len(rtf1.Text)
  189. regxonly
  190. rtf1.SelStart = 1
  191. rtf1.Visible = True
  192. Me.MousePointer = vbNormal
  193. MsgBox Time - starttime
  194. End Sub
  195. Private Sub Form_Load()
  196. apppath = App.Path & IIf(Right(App.Path, 1) = "\", "", "\")
  197. rtf1.LoadFile apppath & "test.html"
  198. ' Define highlight colors
  199. color(0) = vbBlue  'Tag color
  200. color(1) = vbRed    'Tag property color
  201. color(2) = &H8000&  'Tag property value color
  202. color(3) = vbMagenta   'comment Tag color
  203. End Sub
  204. Private Sub Command1_Click()
  205. colorhtml
  206. End Sub
  207. Private Sub Command2_Click()
  208. starttime = Time
  209. Me.MousePointer = vbHourglass
  210. rtf1.Visible = False ' comment out to watch colorize function in action
  211. rtf1.SelStart = 0
  212. rtf1.SelLength = Len(rtf1.Text)
  213. colorhtml
  214. rtf1.SelStart = 1
  215. rtf1.Visible = True ' comment out to watch colorize function in action
  216. Me.MousePointer = vbNormal
  217. MsgBox Time - starttime
  218. End Sub
  219. Private Sub Command3_Click()
  220. rtf1.TextRTF = rtf1.Text
  221. Me.lbltagcount.Caption = ""
  222. End Sub
  223. Private Sub Form_Resize()
  224. rtf1.Left = 0
  225. rtf1.Width = Form1.ScaleWidth
  226. rtf1.Height = Form1.ScaleHeight - 500
  227. Command1.Top = Form1.ScaleHeight - 400
  228. Command2.Top = Command1.Top
  229. Command3.Top = Command1.Top
  230. cmdregx.Top = Command1.Top
  231. lbltagcount.Top = Command1.Top
  232. End Sub
  233. Private Sub rtf1_KeyPress(KeyAscii As Integer)
  234. If Chr(KeyAscii) = "<" Then
  235.     rtf1.SelColor = color(0)
  236. End If
  237. If INtag = True Then
  238.     If Chr(KeyAscii) = " " Then
  239.         If INpropval Then
  240.             rtf1.SelColor = color(2)
  241.         Else
  242.             rtf1.SelColor = color(1)
  243.         End If
  244.     ElseIf Chr(KeyAscii) = """" Then
  245.             rtf1.SelColor = color(2)
  246.     ElseIf Chr(KeyAscii) = ">" Then
  247.             rtf1.SelColor = color(0)
  248.     ElseIf Chr(KeyAscii) = "!" Then
  249.             rtf1.SelColor = color(3)
  250.     End If
  251. End If
  252. End Sub
  253. Private Sub rtf1_KeyUp(KeyCode As Integer, Shift As Integer)
  254. If KeyCode & Shift = "1901" Then ' user pressed >
  255.     rtf1.SelColor = vbBlack
  256. End If
  257. End Sub
  258. Private Function INtag() As Boolean
  259. If InStrRev(rtf1.Text, "<", rtf1.SelStart, vbTextCompare) > InStrRev(rtf1.Text, ">", rtf1.SelStart, vbTextCompare) Then INtag = True
  260. End Function
  261. Private Function INpropval() As Boolean
  262. Dim x, y As Long
  263. x = InStrRev(rtf1.Text, """", rtf1.SelStart, vbTextCompare)
  264. y = InStrRev(rtf1.Text, "=", rtf1.SelStart, vbTextCompare)
  265. If x > y Then
  266. If InStrRev(rtf1.Text, """", x - 1, vbTextCompare) < InStrRev(rtf1.Text, "=", x - 1, vbTextCompare) Then INpropval = True
  267. End If
  268. End Function
  269.